package com.ezcoding.controller;

import com.ezcoding.service.FinanceService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;

@Before(Tx.class)
public class FinanceController extends Controller {
    public void addFinanceItem() {
        String companyId = getPara("companyId");
        int type = getParaToInt("type");//0:收入 1:支出
        String category = getPara("category");
        String subject = getPara("subject");

        renderJson(FinanceService.getInstance().addFinanceItem(
                companyId,
                type,
                category,
                subject));
    }

    public void getFinanceItems() {
        String companyId = getPara("companyId");

        renderJson(FinanceService.getInstance().getFinanceItems(companyId));
    }

    public void addFinanceRecord() {
        String username = getPara("username");
        String companyId = getPara("companyId");
        String itemId = getPara("itemId");
        double money = Double.parseDouble(getPara("money", "0.0"));
        String remark = getPara("remark");

        renderJson(FinanceService.getInstance().addFinanceRecord(username, companyId, itemId, money, remark));
    }

    public void deleteFinanceItem() {
        String companyId = getPara("companyId");
        String itemId = getPara("itemId");

        renderJson(FinanceService.getInstance().deleteFinanceItem(companyId, itemId));
    }

    public void updateFinanceItem() {
        String companyId = getPara("companyId");
        String itemId = getPara("itemId");
        int type = getParaToInt("type");//0:收入 1:支出
        String category = getPara("category");
        String subject = getPara("subject");

        renderJson(FinanceService.getInstance().updateFinanceItem(companyId, itemId, type, category, subject));
    }

    public void financeQuery() {
        String username = getPara("username");
        //type1 0:对外财务 1:对内财务
        int type1 = getParaToInt("type1");
        //type2 详细财务类别
        int type2 = getParaToInt("type2");
        String startDate = getPara("startDate");
        String endDate = getPara("endDate");

        renderJson(FinanceService.getInstance().financeQuery(
                username,
                type1,
                type2,
                startDate,
                endDate));
    }
}
